console.log = function (str) {
    if (console.__debug__ == 1) {
        print(str)
    }
}

let Store = require("./store")
let Floaty = require("./floaty")
let Animations = require("./animations")
let Templace = require("./templace")
let Paojiaoyun = require("./paojiaoyun")

function main () {
    //实例继承
    Store.call(this)
    Floaty.call(this)
    Animations.call(this)
    Templace.call(this)
}

//原型链继承
for (let prop in Store.prototype) {
    main.prototype[prop] = Store.prototype[prop]
}

//初始化
main.prototype.init = function (data) {
    for (d in data) this.store.__need_server__[d] = data[d];
    if (this.store.__need_server__.deBug == 1) console.__debug__ = 1;
    console.log("init")
    //获取所需服务
    this.mutations.wait_server()
    this.window = floaty.rawWindow(this.__floaty_xml__)
    this.__floaty_fun__()
    this.store.__status__.init = true

    //数据监控
    // console.log(Object)
    Object.observe(this.store.__need_switch__, function (changes) {
        switch (changes[0].object.fromChecked) {
            case 1:
                this.window.from.setVisibility(android.view.View.VISIBLE)

            break;
            case 0:
                this.window.from.setVisibility(android.view.View.GONE)
            break;
        }

        switch (changes[0].object.userChecked) {
            case 1:
                this.window.user.setVisibility(android.view.View.VISIBLE)
            break;
            case 0:
                this.window.user.setVisibility(android.view.View.GONE)
            break;
        }
    }.bind(this))
    setInterval(() => {}, 1000)
    console.log("init ok")
}

//绘制UI
main.prototype.drawUI = function (xml, fun) {
    if (!this.store.__status__.init) return "not init";
    console.log("drawUI")

    //导入模板
    this.store.__obj__.config_window = floaty.rawWindow(this.templace.config_t_xml)
    this.store.__obj__.config_window.setSize(0, 0)

    // console.log(this.store.__obj__.config_window)
    
    //载入事件
    this.templace.config_t_cli()
    //载入用户UI
    ui.run(function() {
        //设置圆角
        this.dh.setPopRadio(this.store.__obj__.config_window.temp_main, "#FF009788")
        ui.inflate(xml, this.store.__obj__.config_window.user_body, true)
        //提取用户id
        console.log("用户id")
        // console.log(xml.toString())
        let res = xml.toString().match(/bh_\w+/g)
        this.store.__ids__ = res
        //执行用户事件
        fun(this.store.__obj__.config_window)

        //补全用户view内容
        this.mutations.completion_view()

        
    }.bind(this))
    this.store.__need_switch__.fromChecked = 1
}

//登录泡脚云
main.prototype.login = function (data) {
    if (!this.store.__status__.init) return "not init";
    console.log("login")
    for (let d in data) this.store.paojiaoyun[d] = data[d];
    console.log(this.store.paojiaoyun)
    
    //实例化
    this.pjysdk = new Paojiaoyun(this.store.paojiaoyun.key, this.store.paojiaoyun.secret)
    this.pjysdk.debug = false;
    //开启心跳
    this.pjysdk.event.on("heartbeat_failed", function (hret) {
        console.log(hret.message)
        if (hret.code === 10214) {
            sleep(200);
            exit();  // 退出脚本
        }
        console.log("心跳失败，尝试重登...")
        sleep(2000);
        let login_ret = this.pjysdk.CardLogin();
        this.storage.put("pjysdk_login_ret", login_ret)
        if (login_ret.code === 0) {
            log("重登成功");
            this.mutations.pjysdk_updata(login_ret.result)
        } else {
            toastLog(login_ret.message);  // 重登失败
            sleep(200);
            exit();  // 退出脚本
        }
    }.bind(this))
    
    // 当Autojs脚本正常或者异常退出时会触发exit事件
    events.on("exit", function(){
        this.pjysdk.CardLogout(); // 调用退出登录 某些版本可能需要开新线程调用Logout

        log("结束运行");
    }.bind(this))

    console.log("pjysdk version: "+this.pjysdk._lib_version)
    
    //导入模板
    this.store.__obj__.user_window = floaty.rawWindow(this.usercs.config_t_xml)

    this.store.__obj__.user_window.setSize(0, 0)
    

    //设置圆角
    this.dh.setPopRadio(this.store.__obj__.user_window.temp_main, "#FF009788")
    
    //载入事件
    this.usercs.config_t_cli()

    this.store.__need_switch__.userChecked = 1

}

//运行
main.prototype.run = function (data) {
    if (!this.store.__status__.init) return "not init";

    console.log("run")
    this.store.__script_path__ = data
    this.store.__status__.run = true
    console.log("run ok")

}

//导出
module.exports = new main()
